package com.zr.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author: peterChen
 * @date: 2019/11/22 11:31
 * @description: com.zr.utils
 * @version: 1.0
 */
public class FilesUtils {
    /**
     * 判断文件大小
     *
     * @param len
     *            文件长度
     * @param size
     *            限制大小
     * @param unit
     *            限制单位（B,K,M,G）
     * @return
     */
    public static boolean checkFileSize(MultipartFile file, int size, String unit) {
        long len =file.getSize();
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }else if(fileSize == 0){
            return false;
        }
        return true;
    }
}
